package org.lynn.money;

import javax.swing.*;
import java.awt.*;

/**
 * @author gy
 * @date 2019/6/22 11:22
 */
public class AlgoFrame extends JFrame {

    private int canvasWidth;
    private int canvasHeight;

    public AlgoFrame(String title, int canvasWidth, int canvasHeight) {

        super(title);

        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;

        AlgoCanvas canvas = new AlgoCanvas();
        setContentPane(canvas);

        setResizable(false);
        pack();    // 在setResizable(false)后进行pack()，防止在Windows下系统修改frame的尺寸
        // 具体参见：http://coding.imooc.com/learn/questiondetail/26420.html

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public AlgoFrame(String title) {

        this(title, 1024, 768);
    }

    public int getCanvasWidth() {
        return canvasWidth;
    }

    public int getCanvasHeight() {
        return canvasHeight;
    }

    private int[] moeny;

    public void render(int[] moeny) {
        this.moeny = moeny;
        repaint();
    }

    private class AlgoCanvas extends JPanel {

        public AlgoCanvas() {
            // 双缓存
            super(true);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;

            // 抗锯齿
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.addRenderingHints(hints);

            // 具体绘制

            int w = canvasWidth / moeny.length;
            for (int i = 0; i < moeny.length; i++) {
                if (moeny[i] > 0) {
                    AlgoVisHelper.setColor(g2d, AlgoVisHelper.BLUE);
                    AlgoVisHelper.fillRectangle(g2d, i * w + 1, canvasHeight / 2 - moeny[i], w - 1, moeny[i]);
                } else if (moeny[i] < 0) {
                    AlgoVisHelper.setColor(g2d, AlgoVisHelper.RED);
                    AlgoVisHelper.fillRectangle(g2d, i * w + 1, canvasHeight / 2, w - 1, Math.abs(moeny[i]));
                }

            }

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(canvasWidth, canvasHeight);
        }
    }
}

